//
//  QuickaboutViewController.m
//  WE
//
//  Created by 要帅气的生活 on 15/11/20.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "QuickaboutViewController.h"
#import "Size.h"
#import "QuickaboutLongdistancetoseeadoctorTableViewCell.h"
#import "QuickaboutLongdistancetoseeadoctor.h"
#import "QuickaboutClinicalTimeTableViewCell.h"
#import "QuickaboutDiagnosisofdiseaseTableViewCell.h"
#import "QuickaboutSecuritycodeTableViewCell.h"
#import "QuickaboutpPictureTableViewCell.h"
#import "SubmitTailView.h"
#import "DatPpickerViewController.h"
#import "SingletonStoreSimpleData.h"
#import "QuickaboutDiagnosisofdiseaseSternView.h"

static float gao;
@interface QuickaboutViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,QuickaboutDiagnosisofdiseaseSternDelehate>


{
    BaseView *shortlyView;
    UIView *otherView;
    BaseView *joinView;
    UIView *chooseView;
    QuickaboutDiagnosisofdiseaseSternView *diagnos;
}
@property (nonatomic,strong) QuickaboutClinicalTimeTableViewCell *clinical;
@property (nonatomic,strong) QuickaboutDiagnosisofdiseaseTableViewCell *diagCell;

@end

@implementation QuickaboutViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    [self registerForKeyboardNotifications];
    [SingletonStoreSimpleData shareViewController].TheEndOfTime = nil;
    [SingletonStoreSimpleData shareViewController].TheStartTime = nil;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)registerForKeyboardNotifications
{
    //使用NSNotificationCenter 鍵盤出現時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    //使用NSNotificationCenter 鍵盤隐藏時
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
    
    
}

-(void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    gao = kbSize.height;
    NSLog(@"hight_hitht:%f",kbSize.height);
    if (WindowHeight < 667) {
        [_table setContentOffset:CGPointMake(0, gao+55) animated:YES];
    }else{
        [_table setContentOffset:CGPointMake(0, gao-26) animated:YES];
    }
    [UIView animateWithDuration:0.1 animations:^
     {
         diagnos.frame = CGRectMake(0, WindowHeight-gao-40, WindowWidth, 40);
         _table.frame = CGRectMake(0, 0, WindowWidth, WindowHeight-gao-40);
         
     }];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [self btnClick];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"微医快约";
    self.navigationController.navigationBarHidden = NO;
    
    [self Table];
}

-(void)Table{
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 100)];
    img.backgroundColor = DEBUG_CLOLR;
    
    SubmitTailView *submitTail = [[SubmitTailView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 90)];
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight) style:UITableViewStyleGrouped];
    [self.view addSubview:_table];
    _table.delegate = self;
    _table.dataSource = self;
    _table.separatorStyle = NO;
    _table.tableHeaderView = img;
    _table.tableFooterView = submitTail;
    //_table.userInteractionEnabled = NO;
    shortlyView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight - 200*Height)];
    shortlyView.backgroundColor = [UIColor blackColor];
    shortlyView.hidden = YES;
    shortlyView.alpha = 0.8;
    [shortlyView addTarget:self selector:@selector(pressShortlyView)];
    [self.view addSubview:shortlyView];
    
    joinView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight - 180*Height)];
    joinView.backgroundColor = [UIColor blackColor];
    joinView.hidden = YES;
    joinView.alpha = 0.8;
    [joinView addTarget:self selector:@selector(pressJoinView)];
    [self.view addSubview:joinView];
    
    otherView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 200*Height, WindowWidth, 200*Height)];
    otherView.backgroundColor = [UIColor blackColor];
    otherView.hidden = YES;
   // otherView.alpha = 0.8;
    [self.view addSubview:otherView];
    
    
    UILabel *joinLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 40*Height)];
    joinLabel.text = @"是否接受异地就诊";
    joinLabel.textAlignment = NSTextAlignmentCenter;
    joinLabel.textColor = [UIColor whiteColor];
    joinLabel.font = FONT(14);
    [otherView addSubview:joinLabel];
    UIButton *joinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    joinButton.frame = CGRectMake(10*Width, 50*Height, 355*Width, 40*Height);
    [joinButton setTitle:@"接受" forState:UIControlStateNormal];
    joinButton.titleLabel.font = FONT(14);
    [joinButton addTarget:self action:@selector(pressJoinButton) forControlEvents:UIControlEventTouchUpInside];
    joinButton.backgroundColor = [UIColor whiteColor];
    [joinButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [otherView addSubview:joinButton];
    
    UIButton *noButton = [UIButton buttonWithType:UIButtonTypeCustom];
    noButton.frame = CGRectMake(10*Width, 100*Height, 355*Width, 40*Height);
    [noButton setTitle:@"不接受" forState:UIControlStateNormal];
    noButton.titleLabel.font = FONT(14);
    [noButton addTarget:self action:@selector(pressNoButton) forControlEvents:UIControlEventTouchUpInside];
    noButton.backgroundColor = [UIColor whiteColor];
    [noButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [otherView addSubview:noButton];
    
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleButton.frame = CGRectMake(10*Width, 150*Height, 355*Width, 40*Height);
    [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    cancleButton.titleLabel.font = FONT(14);
    [cancleButton addTarget:self action:@selector(pressCancleButton) forControlEvents:UIControlEventTouchUpInside];
    cancleButton.backgroundColor = [UIColor whiteColor];
    [cancleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [otherView addSubview:cancleButton];
    
    
    chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight - 180*Height, WindowWidth, 180*Height)];
    chooseView.backgroundColor = [UIColor blackColor];
    chooseView.hidden = YES;
    [self.view addSubview:chooseView];
    
    UIButton *chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseButton.frame = CGRectMake(10*Width, 10*Height , 355*Width, 40*Height);
    [chooseButton setTitle:@"选择就诊人" forState:UIControlStateNormal];
    chooseButton.titleLabel.font = FONT(14);
    [chooseButton addTarget:self action:@selector(pressChooseButton) forControlEvents:UIControlEventTouchUpInside];
    chooseButton.backgroundColor = [UIColor blackColor];
    [chooseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chooseView addSubview:chooseButton];
    
    UIButton *noCButton = [UIButton buttonWithType:UIButtonTypeCustom];
    noCButton.frame = CGRectMake(10*Width, 60*Height, 355*Width, 40*Height);
    [noCButton setTitle:@"添加就诊人" forState:UIControlStateNormal];
    noCButton.titleLabel.font = FONT(14);
    [noCButton addTarget:self action:@selector(pressnoCButton) forControlEvents:UIControlEventTouchUpInside];
    noCButton.backgroundColor = [UIColor blueColor];
    [noCButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chooseView addSubview:noCButton];
    
    UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cButton.frame = CGRectMake(10*Width, 110*Height, 355*Width, 40*Height);
    [cButton setTitle:@"取消" forState:UIControlStateNormal];
    cButton.titleLabel.font = FONT(14);
    [cButton addTarget:self action:@selector(pressCButton) forControlEvents:UIControlEventTouchUpInside];
    cButton.backgroundColor = [UIColor grayColor];
    [cButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [chooseView addSubview:cButton];
    
    
    diagnos = [[QuickaboutDiagnosisofdiseaseSternView alloc] initWithFrame:CGRectMake(0, WindowHeight, WindowWidth, 40)];
    diagnos.delegate = self;
    [self.view addSubview:diagnos];
}

- (void)pressNoButton{
    
}

- (void)pressJoinButton{
    
}

- (void)pressCancleButton{
    
}

- (void)pressChooseButton{
    
}

- (void)pressnoCButton{
    shortlyView.hidden = YES;
    otherView.hidden = YES;
}

- (void)pressCButton{
    joinView.hidden = YES;
    chooseView.hidden = YES;

}

- (void)pressJoinView{
    joinView.hidden = YES;
    chooseView.hidden = YES;
}

- (void)pressShortlyView{
    shortlyView.hidden = YES;
    otherView.hidden = YES;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return 1;
    }else if (section == 3){
        return 1;
    }else if (section == 4){
        return 1;
    }else{
        return 0;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        QuickaboutLongdistancetoseeadoctorTableViewCell *quick = [tableView dequeueReusableCellWithIdentifier:@"quickone"];
        if (quick == nil) {
            quick = [[QuickaboutLongdistancetoseeadoctorTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"quickone"];
        }
        quick.label.text = @"是否接受异地就诊";
        quick.right.text = @"接受";
        //[quick.base addTarget:self selector:@selector(jiuzhenchengshi)];
        return quick;
    }else if (indexPath.section == 1){
        _clinical = [tableView dequeueReusableCellWithIdentifier:@"clinicdl"];
        if (_clinical == nil) {
            _clinical = [QuickaboutClinicalTimeTableViewCell new];
        }
        _clinical.label.text = @"期望就诊时间";
        [_clinical.baseL addTarget:self selector:@selector(riqi:)];
        [_clinical.baseR addTarget:self selector:@selector(riqi:)];
        return _clinical;
    }else if (indexPath.section == 2){
        _diagCell = [tableView dequeueReusableCellWithIdentifier:@"diagnos"];
        if (_diagCell == nil) {
            _diagCell = [QuickaboutDiagnosisofdiseaseTableViewCell new];
        }
        _diagCell.label.text = @"请输入不适应状和诊断,发病时间,发病病因(诱因),就诊及治疗经过,既往疾病.想获得什么样的帮助等,方便医生匹配专家团队(请输入10-500个字)";
        _diagCell.field.delegate = self;
        return _diagCell;
    }else if (indexPath.section == 3){
        QuickaboutpPictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"security"];
        if (cell == nil) {
            cell = [QuickaboutpPictureTableViewCell new];
        }
        cell.imgLabel.text = @"添加图片(不超过9张)";
        cell.place.text = @"病症部位、检查报告单、诊断报告单及病历等其他病情资料";
        return cell;
    }else if (indexPath.section == 4){
        QuickaboutSecuritycodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"security"];
        if (cell == nil) {
            cell = [QuickaboutSecuritycodeTableViewCell new];
        }
        //cell.textLabel.text = @"789";
        return cell;
    }else{
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 1 || section == 2) {
        return 60*Height;
    }else{
        return 0.1;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 1;
    }else{
        return 20*Height;
    }
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 60;
    }
    else if (indexPath.section == 1) {
        return 60;
    }
    else if (indexPath.section == 2) {
        return 160;
    }
    else if (indexPath.section == 3) {
        return 100;
    }
    else if (indexPath.section == 4) {
        return 60;
    }
    else{
        return 0;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *arr = @[@"希望就诊城市",@"就诊人",@"确诊疾病"];
    NSArray *array = @[@"请选择",@"请选择就诊人",@"未确诊可不填"];
    if (section == 0) {
        QuickaboutLongdistancetoseeadoctor *view = [[QuickaboutLongdistancetoseeadoctor alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 60)];
        view.label.text = arr[section];
        view.right.text = array[section];
        view.tag = 390;
        [view addTarget:self selector:@selector(xuanzhe:)];
        return view;
        
    }else if (section == 1)
    {
        QuickaboutLongdistancetoseeadoctor *view = [[QuickaboutLongdistancetoseeadoctor alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 60)];
        view.label.text = arr[section];
        view.right.text = array[section];
        view.tag = 391;
        [view addTarget:self selector:@selector(xuanzhe:)];
        return view;
    }else if (section == 2)
    {
        QuickaboutLongdistancetoseeadoctor *view = [[QuickaboutLongdistancetoseeadoctor alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 60)];
        view.label.text = arr[section];
        view.right.text = array[section];
        view.tag = 392;
        [view addTarget:self selector:@selector(xuanzhe:)];
        return view;
    }else
    {
        return nil;
    }
    
}
//选择日期
-(void)riqi:(BaseView *)button{
    if (button.tag == 75) {
        
        void (^suml)(NSString *) = ^(NSString *b){
            [SingletonStoreSimpleData shareViewController].TheStartTime = b;
            _clinical.left.text = b;
        };
        DatPpickerViewController *data = [DatPpickerViewController new];
        data.suml = suml;
        [self presentViewController:data animated:YES completion:^{
            
        }];
        
    }
    if (button.tag == 76) {
        
        void (^suml)(NSString *) = ^(NSString *b){
            [SingletonStoreSimpleData shareViewController].TheEndOfTime = b;
            _clinical.right.text = b;
        };
        DatPpickerViewController *data = [DatPpickerViewController new];
        data.suml = suml;
        
        [self presentViewController:data animated:YES completion:^{
            
        }];
    }
}

//选择城市,人,确诊疾病
-(void)xuanzhe:(QuickaboutLongdistancetoseeadoctor *)button{
    if (button.tag == 390) {
        NSLog(@"希望就诊城市");
        [self presentViewController:[[CityViewController alloc] init] animated:YES completion:nil];
        
        
    }
    if (button.tag == 391) {
        NSLog(@"就诊人");
        
        chooseView.hidden = NO;
        joinView.hidden = NO;
    }
    if (button.tag == 392) {
        NSLog(@"确诊疾病");
    }
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
    }
    if (indexPath.section == 0) {
        NSLog(@"是否接受异地就诊");
        shortlyView.hidden = NO;
        otherView.hidden = NO;
    }
}

-(void)btnClick{
    NSRange range = NSMakeRange([_diagCell.field.text length] - 1, 1);
    [_diagCell.field scrollRangeToVisible:range];
    [_diagCell endEditing:YES];
    diagnos.frame = CGRectMake(0, WindowHeight, WindowWidth, 40);
    _table.frame = CGRectMake(0, 0, WindowWidth, WindowHeight);
}
- (void)textViewDidChange:(UITextView *)textView;

{
    if ([ToolUtil isBlank:textView.text]) {
        _diagCell.label.hidden = NO;
    }else {
        _diagCell.label.hidden = YES;
    }
}
-(void)lefts{
    
}
-(void)rights{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
